from os import name

from flask_wtf import FlaskForm
from marshmallow import Schema, fields, pre_dump
from wtforms.fields.simple import StringField
from wtforms.validators import DataRequired, Length, Optional

from internal.model import DataSet
from pkg.resp.response import message
from pkg.pageinator import PaginatorReq


class DatasetSchema(FlaskForm):
    name = StringField('mame', validators=[
        DataRequired(message="知识库名称不能为空"),
        Length(max=100, message="名称不能超过100")
    ])
    icon = StringField('icon', validators=[
        DataRequired("图标不能为空"),
        Length(max=1000, message="图标字符长度不能超过1000")
    ])
    description = StringField('description', default="", validators=[
        Optional(),
        Length(max=1000, message="描述不能超过1000个字符")
    ])


class DatasetWithPageReq(PaginatorReq):
    serch_keyword = StringField("serch_keyword",
                                validators=[Optional()])


class DatasetResponseSchemaList(Schema):
    id = fields.UUID()
    name = fields.String(dump_default="")
    icon = fields.String(dump_default="")
    created_at = fields.Integer(dump_default=0)

    @pre_dump
    def process_data(self, data: DataSet, **kwargs):
        return {
            "id": data.id,
            "name": data.name,
            "icon": data.icon,
            "created_at": data.created_at.timestamp(),
        }


class DatasetResponseSchema(Schema):
    id = fields.UUID()
    name = fields.String(dump_default="")
    icon = fields.String(dump_default="")
    characterCount = fields.Integer(dump_default=0)
    documentCount = fields.Integer(dump_default=0)
    hitCount = fields.Integer(dump_default=0)
    appCount = fields.Integer(dump_default=0)
    created_at = fields.Integer(dump_default=0)

    @pre_dump
    def process_data(self, data: DataSet, **kwargs):
        return {
            "id": data.id,
            "name": data.name,
            "icon": data.icon,
            "characterCount": data.get_character_count,
            "documentCount": data.get_document_count,
            "hitCount": data.get_hit_count,
            "appCount": data.get_app_count,
            "created_at": data.created_at.timestamp(),
        }
